package com.jzg.mvc.biz.cab.config;

import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Page;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.CabConfig;
import com.jzg.common.util.UuidUtil;

@Path(value = "/biz/cab/config", viewPath = "/views/biz/cab/config")
public class CabConfigController extends BaseController {

	protected final Log LOG = Log.getLog(getClass());

	public void index() {
		this.setAttr("cabId", this.getPara("cabId"));
		CabConfig cabConfig = CabConfig.dao.findFirst("select * from "+CabConfig.TABLE_NAME+" where cabId=?",this.getPara("cabId"));
		this.setAttr("cabConfig", cabConfig);
		render("edit.html");
	}

	public void listData() {
		int pageNumber = getParaToInt("page", 1);
		int pageSize = getParaToInt("limit", 10);
		String keyword = getPara("keyword");
		String cabId = getPara("cabId");
		String sqlExceptSelect = " FROM " + CabConfig.TABLE_NAME + " O  WHERE 1=1 ";
		if (!StrKit.isBlank(keyword)) {
			sqlExceptSelect += " AND ( O.LABEL LIKE '%" + keyword + "%' OR   O.CODE '%" + keyword + "%' ) ";
		}
		if (!StrKit.isBlank(cabId)) {
			sqlExceptSelect += " AND O.cabId='"+cabId+"' ";
		}
		sqlExceptSelect += "  ORDER BY   O.SORT asc ";
		Page<CabConfig> page = CabConfig.dao.paginate(pageNumber, pageSize, "select * ", sqlExceptSelect);
		renderLayuiPage(0, page.getList(), "", page.getTotalRow());
	}

	/***
	 * 保存
	 */
	public void save() {
		CabConfig model = getModel(CabConfig.class);
		CabConfig cabConfig = CabConfig.dao.findFirst("select * from "+CabConfig.TABLE_NAME+" where cabId=? ",model.getCabId());
		if(cabConfig!=null) {
			model.setId(cabConfig.getId());
			if (model.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			model.setId(UuidUtil.getUUID());
			if (model.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}
	}

	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() throws Exception {
		String id = getPara("id");
		CabConfig cabConfig = CabConfig.dao.findById(id);
		if (cabConfig != null) {
			if (cabConfig.delete()) {
				renderSuccess("删除成功！");
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
}
